package com.sinogrand.elevator.repository

import android.content.Context
import android.util.Log
import com.sinogrand.elevator.data.models.HomeElevatorListResponse
import com.google.gson.JsonObject
import com.sinogrand.elevator.network.NetworkClient
import com.sinogrand.elevator.utils.LogTags

/**
 * 首页仓库类
 */
class HomeRepository(context: Context) {
    
    private val apiService = NetworkClient.getApiService(context)
    
    /**
     * 获取首页急需维保电梯列表
     */
    suspend fun getHomeElevatorList(
        page: Int = 1,
        size: Int = 10
    ): Result<HomeElevatorListResponse> {
        return try {
            val request = JsonObject().apply {
                addProperty("page", page)
                addProperty("size", size)
            }
            val apiResponse = apiService.getHomeElevatorList(request)
            
            if (apiResponse.code == 0 && apiResponse.data != null) {
                Log.i(LogTags.API, "✅ 获取首页电梯列表成功 - 返回: ${apiResponse.data.list.size}条")
                Result.success(apiResponse.data)
            } else {
                Log.e(LogTags.API, "❌ 获取首页电梯列表失败 - code: ${apiResponse.code}, msg: ${apiResponse.msg}")
                Result.failure(Exception(apiResponse.msg))
            }
        } catch (e: Exception) {
            Log.e(LogTags.API, "💥 获取首页电梯列表异常 - ${e.javaClass.simpleName}: ${e.message}")
            Result.failure(e)
        }
    }
}
